package com.hupu.jrs.components.jockeyjs.util

import android.annotation.TargetApi
import android.graphics.Bitmap
import android.net.http.SslError
import android.os.Build
import android.os.Message
import android.view.KeyEvent
import android.webkit.*

abstract class ForwardingWebViewClient : WebViewClient() {

    protected abstract fun delegate(): WebViewClient?

    protected fun hasDelegate(): Boolean {
        return delegate() != null
    }

    override fun doUpdateVisitedHistory(view: WebView, url: String, isReload: Boolean) {
        if (hasDelegate()) {
            delegate()!!.doUpdateVisitedHistory(view, url, isReload)
        } else {
            super.doUpdateVisitedHistory(view, url, isReload)
        }
    }

    override fun onFormResubmission(view: WebView, dontResend: Message, resend: Message) {
        if (hasDelegate()) {
            delegate()!!.onFormResubmission(view, dontResend, resend)
        } else {
            super.onFormResubmission(view, dontResend, resend)
        }
    }

    override fun onLoadResource(view: WebView, url: String) {
        if (hasDelegate()) {
            delegate()!!.onLoadResource(view, url)
        } else {
            super.onLoadResource(view, url)
        }
    }

    override fun onPageFinished(view: WebView, url: String) {

        if (hasDelegate()) {
            delegate()!!.onPageFinished(view, url)
        } else {
            super.onPageFinished(view, url)
        }
    }

    override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {

        if (hasDelegate()) {
            delegate()!!.onPageStarted(view, url, favicon)
        } else {
            super.onPageStarted(view, url, favicon)
        }
    }

    override fun onReceivedError(view: WebView, errorCode: Int, description: String, failingUrl: String) {
        if (hasDelegate()) {
            delegate()!!.onReceivedError(view, errorCode, description, failingUrl)
        } else {
            super.onReceivedError(view, errorCode, description, failingUrl)
        }
    }

    override fun onReceivedHttpAuthRequest(view: WebView, handler: HttpAuthHandler, host: String,
                                           realm: String) {

        if (hasDelegate()) {
            delegate()!!.onReceivedHttpAuthRequest(view, handler, host, realm)
        } else {
            super.onReceivedHttpAuthRequest(view, handler, host, realm)
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    override fun onReceivedLoginRequest(view: WebView, realm: String, account: String, args: String) {
        if (hasDelegate()) {
            delegate()!!.onReceivedLoginRequest(view, realm, account, args)
        } else {
            super.onReceivedLoginRequest(view, realm, account, args)
        }
    }

    override fun onReceivedSslError(view: WebView, handler: SslErrorHandler, error: SslError) {
        if (hasDelegate()) {
            delegate()!!.onReceivedSslError(view, handler, error)
        } else {
            super.onReceivedSslError(view, handler, error)
        }
    }

    override fun onUnhandledKeyEvent(view: WebView, event: KeyEvent) {

        if (hasDelegate()) {
            delegate()!!.onUnhandledKeyEvent(view, event)
        } else {
            super.onUnhandledKeyEvent(view, event)
        }
    }

    override fun onScaleChanged(view: WebView, oldScale: Float, newScale: Float) {

        if (hasDelegate()) {
            delegate()!!.onScaleChanged(view, oldScale, newScale)
        } else {
            super.onScaleChanged(view, oldScale, newScale)
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse {
        return if (hasDelegate()) {
            delegate()!!.shouldInterceptRequest(view, url)
        } else {
            super.shouldInterceptRequest(view, url)
        }
    }

    override fun shouldOverrideKeyEvent(view: WebView, event: KeyEvent): Boolean {
        return if (hasDelegate()) {
            delegate()!!.shouldOverrideKeyEvent(view, event)
        } else {
            super.shouldOverrideKeyEvent(view, event)
        }
    }
}
